<?php
	include_once("../backend/constant.php");
	session_start();
	if( !isset($_SESSION["loggedUser"]) || $_SESSION["loggedUser"]==null){
		header("Location:../index.php");
		return;
	}
	
	$connection = new mysqli(dbhost, dblogin, dbpwd, database) or	die("Couldn't connect to database!");
	$connection->set_charset("utf8");
	
	$old_content = "";
	$old_title = "";
	$old_abstract = "";
	$old_tag = "";
	$old_active=1;
	$articleId = 0;
	if(isset($_GET["id"])){
		$articleId = $_GET["id"];
		
		$result = $connection->query("select id, title_vn, abstract, content_vn, tag, active from articles where id=".$articleId) or die("Query failed!");
		//$threadId = $connection->thread_id;
		if( ($result != false) && ($row = $result->fetch_assoc()) ){
			$old_title = $row["title_vn"];
			$old_abstract = $row["abstract"];
			$old_content = $row["content_vn"];
			$old_tag = $row["tag"];
			$old_active = $row["active"];
		}
	}
?>
<!DOCTYPE HTML>
<html>
<head>
	<meta charset="utf-8">
	<link rel="stylesheet" type="text/css" href="admin.css"/>
	<script type="text/javascript" src="../tinymce/tinymce.min.js"></script>
	<script type="text/javascript" src="../jquery-min.js"></script>
	<script type="text/javascript">
		tinyMCE.init({
			width:1000,
			height:"360",
			menubar:true,
			plugins: 'link image code',
			selector:"textarea"
		});
		
		$(document).ready(function(){
			$("#btnPost").click(function(){
				var tdata = new FormData();
				
				var tcontent = tinyMCE.get('txtContent').getContent();
				var ttitle = document.getElementById("txtTitle").value;
				var tabstract = document.getElementById("txtAbstract").value;
				var ttag = document.getElementById("hTags").value;
				//var file = $("#fattach")[0].files[0];
				var file = document.getElementById("fAttach").files[0];
				var post_id = document.getElementById("hId").value;
				var isActive = document.getElementById("chkActive").checked;
				var lang = $("input[name=language]:checked").val();
				
				if(ttitle==""){
					alert("Title must not be blank!");
					return false;
				}
				
				if(post_id <= 0)
					tdata.append("ACTION", "POSTARTICLE");
				else{
					tdata.append("ACTION", "UPDATEARTICLE");
					tdata.append("id",post_id);
				}
				tdata.append("txtContent",tcontent);
				tdata.append("txtTitle",ttitle);
				tdata.append("txtAbstract", tabstract);
				tdata.append("language", lang);
				tdata.append("txtTag", ttag);
				tdata.append("attach", file);
				tdata.append("isActive",isActive);
				//alert("Going to post an article 1");
				$.ajax({
					url:"backend/post.php",
					cache:false,
					type:"POST",
					contentType:false,
					data:tdata,
					processData:false,
					success:function(responseText){
						//alert(responseText);
						
						var rs = $.parseJSON(responseText);
						if(rs.status == "OK"){
							//alert("Post article OK.");
							document.getElementById("txtTitle").value = "";
							document.getElementById("txtAbstract").value = "";
							//tinyMCE.get('txtContent').setContent("");
							tinyMCE.activeEditor.setContent('');
						}else{
							alert(rs.status);
						}
						
					}
				});
				return false;
			});
			
			$("#div_main").on("click", "a.delcls", function(){
				var linkId = $(this).attr("id");
				//alert(linkId);
				$.ajax({
					url:"backend/post.php",
					cache:false,
					type:"POST",
					data:{ACTION:"DELETEFILE", fileId:linkId},
					success:function(responseText){
						var rs = $.parseJSON(responseText);
						//alert(responseText);
						if(rs.status == "OK"){
							$("#file"+linkId).remove();
						}
					}
				});
				return false;
			});
			
			$("#div_main").on("click", "a.post-tag", function(){
				var tag = $(this).text();
				//alert(tag);
				var cdiv = document.getElementById("span-post-tag-list");
				var newTag = document.createElement("a");
				var tags = $("#hTags").val();
				
				if(tags == "undefined" || tags == "")
					$("#hTags").val(tag)
				else
					$("#hTags").val(tags + "," + tag)
				
				
				$(newTag).attr("href","aaa");
				$(newTag).addClass("selected-post-tag");
				newTag.innerHTML = tag;
				cdiv.appendChild(newTag);
				$(this).remove();
				//alert($("#hTags").val());
				return false;
			});
			
			$("#div_main").on("click", "a.selected-post-tag", function(){
				var tag = $(this).text();
				//alert(tag);
				var cdiv = document.getElementById("tags-list");
				var newTag = document.createElement("a");
				var hTags = $("#hTags").val();
				var arrtag = hTags.split(",");
				var newTags = "";
				
				for(i=0;i<arrtag.length;i++){
					if(arrtag[i]!=tag){
						if(newTags=="")
							newTags = arrtag[i];
						else
							newTags = newTags + "," + arrtag[i];
					}
				}
				$("#hTags").val(newTags);
				
				$(newTag).attr("href","aaa");
				$(newTag).addClass("post-tag");
				newTag.innerHTML = tag;
				cdiv.appendChild(newTag);
				$(this).remove();
				//alert($("#hTags").val());
				return false;
			});
		});
	</script>
	<title>VTech blog - Administration</title>
</head>	
<body>
	<div id="container">
			<div id="div_header">
			<div id="div_user_pane">
					<span style="padding-left:30px;color:#dafafa;">Xin chao </span>
					<a href="#" style="text-decoration:none;color:#ffff22;" ><span style="font-weight:bold;"><?php echo $_SESSION["loggedUser"]; ?></span></a>
					<a href="#" style="text-decoration:none;color:#ffdada" id="btnLogout">Logout</a>
				</div>
			</div>
			<div id="div_nav" class="menu">
				<ul>
					<li><a href="../index.php">Home</a></li>
					<li><a href="../funct.php">NMath</a></li>
					<?php
						if(isset($_SESSION["loggedUser"])){
							if($_SESSION["loggedUser"]=='davu' || $_SESSION["loggedUser"]=='admin'|| $_SESSION["loggedUser"]=='huong'){
								print "<li><a href='dashboard.php'>Administrative</a></li>";
							}
						}
					?>
					<li><a href="../about.php">About me</a></li>
				</ul>
			</div>
			<div style="clear:both"/>
			<div id="sidebar">
				<ul>
					<li><a href="dashboard.php">Dashboard</a></li>
					<li><a href="userlst.php">Users</a></li>
					<li><a href="articles.php">Posts</a></li>
					<li><a href="files.php">Files</a></li>
					<li><a href="../about.php">Application</a></li>
				</ul>
			</div>
			<div id="div_main">
				<input type="hidden" id="hId" name="hId" value="<?php print $articleId; ?>" />
				<div id="div-lang">
					<label for="rdVN">Vietnamese</label>
					<input type="radio" title="Vietnamese" id="rdVN" name="language" value="0" checked />
					&nbsp;
					<label for="rdEN">English</label>
					<input type="radio" title="English" id="rdEN" name="language" value="1" />
				</div>
				<div class="post-field" ><div class="post-field-label">Title</div>
					<div class="post-field-input">
						<input type="text" id="txtTitle" style="width:90%" name="txtTitle" value="<?php print $old_title; ?>" />
					</div>
				</div>
				<div class="post-field" ><div class="post-field-label">Abstract</div>
					<div class="post-field-input">
					<input type="text" id="txtAbstract" style="width:90%" name="txtAbstract" value="<?php print $old_abstract; ?>" />
					</div>
				<div>
				<textarea id="txtContent"><?php print stripslashes($old_content); ?></textarea>
				Choose tag for this post by clicking one by one from following list&nbsp;
				<div class="post-field-input-tags">
					<input type="hidden" id="hTags" name="hTags" value="<?php print $old_tag; ?>" />
					<div id="span-post-tag-list">
						<?php
							$tags = preg_split("/,/", $old_tag, -1, PREG_SPLIT_NO_EMPTY);
							for($i=0;$i<count($tags);$i++){
								print "<a href=\"#\" class=\"selected-post-tag\" >" . $tags[$i] . "</a>";
							}
						?>
					</div>
				</div>
				<div class="post-tags" id="tags-list">
					<?php
						$arrConstantTags = array(0=>'c/c++','java','javascript','uml','c#','algorithm',
						'vb','vba','linux','windows','atl','eclipse','rcp','computer-vision','php');
						$isExisted = false;
						for($i=0;$i<count($arrConstantTags);$i++){
							$isExisted = false;
							for($j=0;$j<count($tags);$j++) //dang lam o day
								if($arrConstantTags[$i]==$tags[$j]){
									$isExisted = true;
									break;
								}
							if(!$isExisted)
								print "<a href=\"#\" class=\"post-tag\" id=\"tag-item1\" >" . $arrConstantTags[$i] . "</a>";
						}
					?>
					<!--
					<a href="#" class="post-tag" id="tag-item1" >c/c++</a>
					<a href="#" class="post-tag" id="tag-item2" >java</a>
					<a href="#" class="post-tag" id="tag-item3" >c#</a>
					<a href="#" class="post-tag" id="tag-item4" >algorithm</a>
					<a href="#" class="post-tag" id="tag-item5" >vb</a>
					<a href="#" class="post-tag" id="tag-item6" >vba</a>
					<a href="#" class="post-tag" id="tag-item7" >linux</a>
					<a href="#" class="post-tag" id="tag-item8" >windows</a>
					<a href="#" class="post-tag" id="tag-item9" >ATL</a>
					<a href="#" class="post-tag" id="tag-item10" >Eclipse</a>
					<a href="#" class="post-tag" id="tag-item11" >RCP</a>
					-->
				</div>
				Active&nbsp;<input type="checkbox" style="width:100%" id="chkActive" name="chkActive" <?php print ($old_active==1)?"checked":"";?> />
				<?php
					if(isset($articleId) && $articleId>0){
						$resultFiles = $connection->query("select id, filename, mediatype, url, active from media where article=".$articleId) or die("Query failed!");
						while($resultFiles && $row=$resultFiles->fetch_assoc()){
							$url = $row["url"];
							if($url==null || $url==""){
								$url = "../files/".$row["filename"];
							}
							print "<div id=\"file".$row["id"]."\">";
							print "<a href=\"".$url."\">" . $row["filename"] . "</a>&nbsp;<a href=\"#\" class=\"delcls\" id=\"".$row["id"]."\">delete</a>";
							print "</div>";
						}
					}
				?>
				<input type="file" style="width:100%" id="fAttach" name="fAttach"/>
				<center><input type="button" id="btnPost" name="btnPost" value="Post"></input></center>
				<div id="preview">
				</div>
			</div>
			<div id="div_footer" >Copyright 2013 @Dao Anh Vu</div>
		</div>
<body>
</html>
<?php
	$connection->close();
?>
